﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using System.Text;
using ttbit;
using ttbit.form;
using static System.Net.Mime.MediaTypeNames;

namespace lib.bitform.test
{
    public class TestFont
    {
        public FontData fontdata
        {
            get;
            private set;
        }
        ITexture texFont;
        Dictionary<char, Rectangle> chars = new Dictionary<char, Rectangle>();
        int fontx = 0;
        int fonty = 0;

        public void LoadFont(byte[] filestream)
        {


            texFont = Env.Graphic.CreateDynamicTexture(256, 256, TextureFormat.R8);
            using System.IO.MemoryStream ms = new System.IO.MemoryStream(filestream);
            using System.IO.BinaryReader reader = new System.IO.BinaryReader(ms);
            fontdata = new FontData();
            fontdata.Read(reader);

            Env.Log("font charcount=" + fontdata.header.charCount + " fontsize=" + fontdata.header.fontSize);
        }
        public void EnsureFont(string text)
        {
            int fontbitsize = (int)fontdata.header.fontBitmapSize;
            foreach (var c in text)
            {
                if (chars.ContainsKey(c))
                    continue;
                if (fontdata.fontItems.TryGetValue((uint)c, out var item))
                {
                    chars[c] = new Rectangle(fontx, fonty, fontbitsize, fontbitsize);
                    byte[] flip = new byte[fontbitsize * fontbitsize];
                    for (var by = 0; by < fontbitsize; by++)
                    {
                        for (var bx = 0; bx < fontbitsize; bx++)
                        {
                            flip[by * fontbitsize + bx] = item.graydata[by * fontbitsize + bx];
                        }
                    }
                    Env.Graphic.UploadTexture(texFont, fontx, fonty, fontbitsize, fontbitsize, flip);
                    fontx += fontbitsize;
                    if (fontx > texFont.width)
                    {
                        fontx = 0;
                        fonty += fontbitsize;
                    }
                }
            }
            Env.Graphic.ApplyTexture(texFont, false);
        }
        public void DrawFont(IBatcher batcher, string text, Vector2 pos)
        {
            EnsureFont(text);

            int x = 0;

            foreach (var c in text)
            {
                if (fontdata.fontItems.TryGetValue((uint)c, out var item))
                {

                    var uv = chars[c];


                    int h = item.height;
                    int w = item.width;
                    if (h > uv.Height)
                        h = uv.Height;
                    if (w >= uv.Width)
                    {
                        w = uv.Width;
                    }

                    int yadd = item.emptyY;// = -h - item.emptyY;
                    batcher.DrawRect(texFont,null,
                        new RectangleF((int)pos.X + x + item.emptyX, (int)pos.Y + yadd, w, h), 
                        new Rectangle(uv.X, uv.Y, w, h),Color32.White);

                    {
                        x += item.advanceX;
                    }
                }
            }
        }
    }
}
